package org.bb.ssm.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.bb.ssm.model.LoginInfo;
import org.bb.ssm.service.LoginInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
//@RequestMapping(value = "/login")

public class LoginInfoCotroller {
	@Autowired
	private LoginInfoService loginInfoService;
	
	

	

	@RequestMapping("/login.action")
	public String login(String loginname,String loginpass,HttpServletRequest httpServletRequest) {

		Map<String, String> map = new HashMap<String, String>();
		map.put("username", loginname);
		map.put("password", loginpass);
		httpServletRequest.getSession().setAttribute("id", loginname);
		String string=loginInfoService.searchByPass(map);
		
		if(string!=null) {
			return "lead";
		}
		else	
		return "index";
		
		

	}

	@RequestMapping("/register.action")
	public String register(String id,String password1) {
		
		LoginInfo loginInfo = new LoginInfo();
		loginInfo.setUserId(Integer.parseInt(id));
		loginInfo.setUserPassword(password1);
		System.out.println(id);
		loginInfoService.addUser(loginInfo);
		
		return "index";
		
	}
	
	@RequestMapping("/updatePass.action")
	public String updatePass() {
		
		return "updatePass";
		
	}
	
	@RequestMapping("/goregister.action")
	public String goregister() {
		
		return "register";
		
	}
	
	@RequestMapping("/gologin.action")
	public String gologin() {
		
		return "index";
		
	}
	
	@RequestMapping("/goExit.action")
	public String goExit() {
		
		return "index";
		
	}
	
	@RequestMapping("/UpdatePass.action")
	public String UpdatePass(HttpServletRequest httpServletRequest,String userPassword) {		
		String userno=(String) httpServletRequest.getSession().getAttribute("id");
		LoginInfo loginInfo=new LoginInfo();
		int id = 0;
		try {
			id = Integer.parseInt(userno);
		} catch (Exception e) {
			System.out.println("解析错误");
		}
		loginInfo.setUserId(id);
		loginInfo.setUserPassword(userPassword);
		loginInfoService.changePass(loginInfo);	
		return "index";
		
	}
	
	@RequestMapping("/goUpdatePass.action")
	public String UpdatePass() {
		
		return "updatePass";
		
	}
	
	@RequestMapping("/registerCheck.action")
	@ResponseBody
	public String registerCheck(String id) {
		System.out.println("111");
		System.out.println(id);
		if (id!="") {
			System.out.println(id);
			int result=loginInfoService.checkIfloginname(id);
			System.out.println(result);
			if (result != 1) {
				System.out.println("进来");
				return "true";
			}else {
				System.out.println("出去");
				return "false";
			}
		}else {
			return "null";
		}	
	}
	
}
